// 插件初始化入口
import { pluginRoutes } from './router'
import PluginHome from './PluginHome.vue'
const pluginBase = '/' + __PLUGIN_NAME__;

export function init() {

}

function addPrefixToRoutes(routes: any, prefix: any) {
  const newRoutes = routes.map((route: any) => {
    const newRoute = {
      ...route,
      path: prefix + (route.path.startsWith('/') ? route.path : '/' + route.path)
    };
    if (route.redirect) {
      if (typeof route.redirect === 'string' && route.redirect.startsWith('/')) {
        newRoute.redirect = prefix + route.redirect;
      } else {
        newRoute.redirect = route.redirect;
      }
    }
    if (route.children) {
      // 子路由的 path 不加前缀，只保留原有结构
      newRoute.children = route.children;
    }
    return newRoute;
  });
  return newRoutes
}

export function register(router: any) {
  const routesWithPrefix = addPrefixToRoutes(pluginRoutes, pluginBase);
  routesWithPrefix.forEach((route: any) => {
    const existingRoute = router.getRoutes().find((r: any) => r.path === route.path);
    if (!existingRoute) {
      router.addRoute(route);
    }
  });
  return {
    routes: routesWithPrefix
  };
}

export { PluginHome }


(window as any)[__PLUGIN_NAME__ as any] = {
  PluginHome,
  register,
  init
}